package com.jeecg.guangdian.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class CommonUtil {
	public static boolean checkDay(Date date,int dayadd) {
		DateFormat format = new SimpleDateFormat("yyyyMMdd");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		String time = new SimpleDateFormat("yyyyMM" + day).format(calendar.getTime());
		try {
			date=format.parse(time);
		}catch (Exception e) {
		  e.printStackTrace();
		}
		Calendar calendar1 = Calendar.getInstance();
		calendar1.setTime(date);
		calendar1.add(Calendar.DAY_OF_MONTH, dayadd);
		int endTime=Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(calendar1.getTime()))-Integer.parseInt(CommonUtil.getCurrentDate("yyyyMMdd"));
		if(endTime>=0)
		{
			return true;
		}else
		{
			return false;
		}
	}
	public static String getoutstartday(int dayadd) {
		 if((dayadd-Integer.parseInt(CommonUtil.getCurrentDate("dd")))>=0)
		 {
			 Calendar calendar = Calendar.getInstance();
			 calendar.setTime(new Date());
			 calendar.add(Calendar.MONTH, -1);
			 return new SimpleDateFormat("yyyy-MM-01").format(calendar.getTime());
		 }
		 else
		 {
			 return CommonUtil.getCurrentDate("yyyy-MM-01");
		 }
	}
	public static String getoutendday(int dayadd) {
		Date date=null;
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		int day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		String time = new SimpleDateFormat("yyyy-MM-" + day).format(calendar.getTime());
		try {
			date=format.parse(time);
		}catch (Exception e) {
		  e.printStackTrace();
		}
		Calendar calendar1 = Calendar.getInstance();
		calendar1.setTime(date);
		calendar1.add(Calendar.DAY_OF_MONTH, dayadd);
		return new SimpleDateFormat("yyyy-MM-dd").format(calendar1.getTime());
	}
	public static String replaceToSqlStr(String str) {
		if (isNull(str)) {
			return "";
		} else {
			StringBuffer bf = new StringBuffer();
			String strs[] = str.split(",");
			for (int i = 0; i < strs.length; i++) {
				bf.append("'" + strs[i] + "'");
				if (strs.length - 1 != i) {
					bf.append(",");
				}
			}
			return bf.toString();
		}
	}
    public static boolean isNull(String strIn) {
        
        if (strIn == null || "".equals(strIn.trim())) {
            return true;
        }

       
        return false;
    }
	public static String getCurrentDate(String format) {
		Calendar calendar = Calendar.getInstance();
		String time = new SimpleDateFormat(format).format(calendar.getTime());

		return time;
	}

	public static Date strToDateLong(String strDate){
		try {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date strtodate = formatter.parse(strDate);
			return strtodate;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static Date strToDate(String strDate){
		try {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date strtodate = formatter.parse(strDate);
			return strtodate;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

